package cn.onein.edu.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.onein.edu.biz.bean.LearnCategory;
import cn.onein.edu.biz.bean.LearnSection;
import cn.onein.edu.web.common.base.BaseController;
import cn.onein.edu.web.service.LearnContentService;
import cn.onein.edu.web.service.LearnService;
import cn.onein.edu.web.util.PerPageUtil;
import cn.onein.edu.web.vo.PerpageVO;

/**
 * 育儿百科首页 控制器
 * @author Justin
 *
 */
@Controller
public class LearnController extends BaseController{

	private final static Integer PERPAGESIZE = 20;
	
	Logger logger = Logger.getLogger(IndexController.class);

	@Autowired
	private LearnService learnService;
	
	@Autowired
	private LearnContentService learnContentService;
	
	private void setHotTags(HttpServletRequest req){
		req.setAttribute(REQUEST_KEY_HOT_TAGS, learnContentService.getHotTags());
	}

	/**
	 * 育儿百科首页显示页面
	 * @return
	 */
	@RequestMapping(value = "/learn", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView index(HttpServletResponse resp, HttpServletRequest req)
			throws Exception {
		Map<String, Object> map = null;
		
		try{
			map = learnService.getLearnIndex();
		}catch(Exception ex){
			logger.error("初始化育儿百科出错!", ex);
		}

		setHotTags(req);
		this.setMenu(map, BaseController.MENU_VALUE_LEARN);
	
		return new ModelAndView("learn/index", map);
	}
	
	/**
	 * 一级目录首页显示页面
	 * @return
	 */
	@RequestMapping(value = "/learn/{sectionName}", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView sectionIndex(@PathVariable String sectionName,HttpServletResponse resp, HttpServletRequest req)
			throws Exception {
		Map<String, Object> map = null;
		
		try{
			map = learnService.getHotContentBySectionName(sectionName, 9);
			
			learnService.getLearnMenu(map);
		}catch(Exception ex){
			logger.error("初始化育儿百科一级目录首页出错!", ex);
		}
		
		if(null == map || map.isEmpty()){
			return new ModelAndView("resourceNotFound");
		}
		
		learnService.getSectionDetail(map, sectionName);

		learnService.getContentGroupByCategory(map,sectionName);
		
		map.put("sectionName", sectionName);
		
		setHotTags(req);
		this.setMenu(map, BaseController.MENU_VALUE_LEARN);
		return new ModelAndView("learn/sectionindex",map);
	}
	
	/**
	 * 二级目录首页显示页面
	 * @return
	 */
	@RequestMapping(value = "/learn/{sectionName}/{categoryName}/{currPage}", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView categoryIndex(@PathVariable String sectionName,@PathVariable String categoryName,@PathVariable Integer currPage,HttpServletResponse resp, HttpServletRequest req)
			throws Exception {
		Integer count = null;
		Map<String, Object> map = new HashMap<String,Object>();
		LearnCategory currCategory;
		
		LearnSection currSection = learnService.getSectionBySectionName(sectionName);
		
		if(null == currSection){
			return new ModelAndView("resourceNotFound");
		}
		
		if(null == currPage){
			currPage = 1;
		}
		
		if(!"ALLCATEGORY".equals(categoryName)){
		    currCategory = learnService.getCurrCategoryByCategoryName(categoryName);
			
		    if(null == currCategory){
				return new ModelAndView("resourceNotFound");
			}
			
//			try{
//				learnService.getHotContentByCategory(map, currCategory.getId(),null);
//			}catch(Exception ex){
//				logger.error("初始化育儿百科二级目录首页出错!", ex);
//				return new ModelAndView("error");
//			}
			
			//得到记录条数
			count = learnService.getContentCountsByCategory(currCategory.getId(),null);
			
			//按照分页得到文章
			learnService.getPerpageContentByCategory(map, currCategory.getId(),null,currPage,PERPAGESIZE);
			
			map.put("category", currCategory.getTitle());
		}else{
			try{
				learnService.getHotContentByCategory(map, null,sectionName);
			}catch(Exception ex){
				logger.error("初始化育儿百科二级目录首页出错!", ex);
				return new ModelAndView("error");
			}
			
			//得到记录条数
			count = learnService.getContentCountsByCategory(null,sectionName);
			
			//按照分页得到文章
			learnService.getPerpageContentByCategory(map,null,sectionName,currPage,PERPAGESIZE);
			
			map.put("category", "所有分类");
		}
		
		try{
			learnService.getLearnMenu(map);
		}catch(Exception e){
			logger.error("Get learn menu error.", e);
			return new ModelAndView("error");
		}
		
		//得到分页BEAN
		PerpageVO perpageVO = PerPageUtil.getPerPageVO(count, currPage, PERPAGESIZE);
		
		this.setMenu(map, BaseController.MENU_VALUE_LEARN);
        map.put("perpageVO", perpageVO);
        map.put("section", currSection.getTitle());
        
        setHotTags(req);
		return new ModelAndView("learn/categoryindex",map);
	}
	
	
}
